package cn.ac.cintcm.sh.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.ac.cintcm.sh.domain.Comment;
import cn.ac.cintcm.sh.domain.Prescription;
import cn.ac.cintcm.sh.domain.View;



public class CommentDao {
	private HsqldbDao dao = (new HsqldbDao()).getDao();
	public int insertDate(Comment comment){
		    dao.openConnection();
	        String sql = "insert into comment(itemId,comment) values( "+comment.getItemId().intValue()+",'"+comment.getComment()+"')";
	        int id = dao.executeInsertKey(sql);
	        dao.execute("shutdown");
	        dao.closeConnection();
		return id;
	}
	
	public int updateDate(Comment comment){
		dao.openConnection();
		String sql = "update comment set  comment='"+comment.getComment()+"' where id="+comment.getId();
        int id = dao.executeUpdate(sql);
        dao.execute("shutdown");
        dao.closeConnection();
		return id;
	}
	
	public int delDate(Long id){
		 dao.openConnection();
	        String sql = "delete from comment where id="+id;
	        dao.executeUpdate(sql);
	        dao.closeConnection();
		return 0;
	}
	
	public List getComment(Long itemId){
		  List<Comment> nodes = new ArrayList<Comment>();
	        String sql="select id, comment from comment where itemId="+itemId;
	        dao.openConnection();
	        ResultSet rs=this.dao.executeQuery(sql);
	        try{
	            while(rs.next()){
	                int id=rs.getInt("id");
	                String commentStr =rs.getString("comment");
	                
	                Comment comment = new Comment();
	                comment.setId(new Long(id));
	                comment.setComment(commentStr);
	                nodes.add(comment);
	            }
	        }catch(Exception e){
	            e.printStackTrace();
	        }
	        dao.closeConnection();
	        return nodes;
	}
}
